﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using PK3Model;
using sqengine.Components;
using sqengine.Engine.Common;
using SqEngine.Audio;
#endregion

namespace sqengine.PhysicObjects
{

    public class PK3_Model : BasicBotObject
    {
        private const double _nohittimespan = 500;

        private double _nothit = _nohittimespan;

        private PlayerStats _player;
        private bool _justhit;
        private bool _died = false;
        private bool _canshoot = true;
        private bool _ammoempty = false;
        private double _reloadtime = 200;
        private Model _bulletmodel;
        private BotMessagesInfo _botmessages;
        float rot = 0.0f;
        Vector3 aerre = new Vector3();

        Vector3 _positionofbullet = new Vector3();

        private SqAudio _audioengine;



        public PlayerStats BotPlayer { get { return _player; } set { _player = value; } }
        public bool JustHit { get { return _justhit; } set { _justhit = value; } }

        public PK3_Model(Game game, CPK3Model model, Vector3 baserotation, Vector3 position, string start_animation, WeaponInfo weaponmodel, AdvancedContent adcontent, string botname, BotMessagesInfo botmessages)
            : base(game, model, baserotation, start_animation, weaponmodel)
        {
            
            Scale = new Vector3(0.05f, 0.05f, 0.05f);

            body = new Body();
            collision = new CollisionSkin(body);

            _audioengine = ((sqengine)game).GetAudioEngine;
            _botmessages = botmessages;

            Box box = new Box(new Vector3(-1, -1.2f, -0.5f), Matrix.Identity, new Vector3(2.5f, 2.7f, 1.5f));
            
            collision.AddPrimitive(box, (int)MaterialTable.MaterialID.NotBouncyNormal);
            
            //Box BndBox = BuildPhysicsBoxFromBoundingBox(GetBoundingBoxScaled(model.getBoundingBox(), Scale), GetMatrixRotation());
           
            //collision.AddPrimitive(BndBox, (int)MaterialTable.MaterialID.NormalNormal);

            body.CollisionSkin = this.collision;
            _player = new PlayerStats(game,1000, position, botname);
            _player.Weapon = weaponinfo;
            //Vector3 com = SetMass(10.0f);
            _player.OnDie += new PlayerStats.ChangingHandler(_player_OnDie);
            body.MoveTo(position, Matrix.Identity);
            body.EnableBody();
            _bulletmodel = adcontent.LoadModel("sphere");

        }

        void _player_OnDie(object sender, PlayerArg ca)
        {
            _died = true;
        }

        public override void ApplyEffects(BasicEffect effect)
        {

        }

        public override void Update(GameTime gameTime)
        {


            KeyboardState kb = Keyboard.GetState();
            if (kb.IsKeyDown(Keys.O))
            {
                //body.CollisionSkin.NewOrient = Matrix.CreateFromYawPitchRoll(0.0f, 0.0f, rot); 
                //body.CollisionSkin.GetPrimitiveLocal(0).Transform.Orientation *= Matrix.CreateFromYawPitchRoll(0.0f, 0.0f, rot);
            }

            //Y rotatione

            if (_justhit == true)
            {
                _nothit -= gameTime.ElapsedGameTime.TotalMilliseconds;
                if (_nothit <= 0)
                {
                    _nothit = _nohittimespan;
                    _justhit = false;

                }
            }

            if (_died)
            {
                ((sqengine)CurrentGame).RemoveBot(this);
                ((sqengine)CurrentGame).AddMessage(((sqengine)CurrentGame).CurrentPlayer.Name + " kill " + _player.Name, Color.Red, 2000, "");
                
            }




            base.Update(gameTime);
        }

        public PhysicObject Shoot(Vector3 pos, Matrix ori)
        {
            if (_canshoot)
            {
                if (!_ammoempty)
                {
                    //PlaySound(_player.Weapon.WeaponShootSound);
                    _reloadtime = 200;
                    return SpawnBullet(pos, ori);
                }
                else
                {//Click!!
                    PlaySound(_player.Weapon.WeaponOutOfAmmoSound);
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        private PhysicObject SpawnBullet(Vector3 pos, Matrix ori)
        {
            PhysicObject physicObj;
            physicObj = new BulletObject(CurrentGame, _bulletmodel, 0.1f, ori, pos, 1f, string.Empty, _player);
            return physicObj;
        }

        private void PlaySound(string filename)
        {
            if (filename != "")
            {

                AudioEmitter _audioemitter = new AudioEmitter();
                AudioListener _audiolistener = new AudioListener();

                _audiolistener.Position = ((sqengine)CurrentGame).Camera.Position;
                _audioemitter.Position = this.body.Position;

                Cue _snd = _audioengine.GetCue(filename);
                _audioengine.Play(_snd, true, _audiolistener, _audioemitter);
            }
        }

    }
}
